package sprinklerbee.views;

import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;


import sprinklerbee.factory.ServiceFactory;
import sprinklerbee.model.Scheduler;
import sprinklerbee.service.SchedulerService;
import sprinklerbee.utils.MemoryCache;
@SuppressWarnings("serial")
public class ControlPanel extends JPanel {

	JFrame mainFrame;

	static DisplayPanel displayPanel;
	ActivityPanel activityPanel;
	InfoPanel infoPanel;

	public ControlPanel() {
		 //Create and set up the window.
		try {
			UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		 mainFrame = new JFrame("SprinklerBee");
		 mainFrame.setResizable(false);
		 BorderLayout borderLayout = (BorderLayout) mainFrame.getContentPane().getLayout();
		 borderLayout.setHgap(10);
		 mainFrame.pack();
		 
		 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 
		 activityPanel = new ActivityPanel();
		 activityPanel.setBorder(null);
		 activityPanel.setBackground(SystemColor.menu);
		 GridLayout gridLayout = (GridLayout) activityPanel.getLayout();
		 gridLayout.setHgap(5);
		 gridLayout.setRows(3);
		 infoPanel = new InfoPanel();
		 infoPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "System Infomation"));
		 infoPanel.setBackground(SystemColor.menu);
		 displayPanel = new DisplayPanel();
		 displayPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Display Panel"));
		 displayPanel.setBackground(SystemColor.menu);
		 //Add the panel to the window.    		      
		 mainFrame.getContentPane().add(displayPanel, BorderLayout.CENTER);
		 mainFrame.getContentPane().add(infoPanel, BorderLayout.SOUTH);
		 mainFrame.getContentPane().add(activityPanel, BorderLayout.WEST);
		 infoPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		 // make the frame 1/2 the height and width
		 //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		 //int width = 2600;
		 //int height = 1400;
		 mainFrame.setSize(1300, 700);
		
		 // center the mainFrame on screen
		 mainFrame.setLocationRelativeTo(null);
		 mainFrame.setVisible(true);
		 
	}
	
	public static void main(String[] args) throws ParseException {
		new MemoryCache();
		new ControlPanel();
		//sensor thread, update every 1 min
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				generateSensorData();
			}
		}, 0,1000*60*2);
		
		//schedule thread, update every 1 min
		final SchedulerService service=ServiceFactory.getSchedulerServiceInstance();
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					service.addScheduleHistory();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		},1000*60*60,1000*60*60*24);
		
		//update status for display panel
		//update sensor panel
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				displayPanel.loadData();
				SensorPanel.updateSensor();
			}
		}, 0,1000*3);
	}  

	private static void generateSensorData(){
		Random randomSensorGen = new Random();
		double rand_temp = randomSensorGen.nextInt(140);
		boolean rand_rain = randomSensorGen.nextBoolean();
		
		double dbl_rain;
		if (rand_rain)
		{
			dbl_rain = 1;
		}
		else
		{
			dbl_rain = 0;
		}
		
		Map<String, String> map=new HashMap<String, String>();
		map.put("temperature",String.valueOf(rand_temp));
		map.put("rain", String.valueOf(dbl_rain));
		if (ServiceFactory.getSensorInstance().updateSensor(map)) {
			System.out.println("Generate random data for sensor!");
			MemoryCache.refreshMemoryCache();
		}
	}
	
}
